import 'package:flutter/material.dart';
import 'package:wallpaper/components/my_tab_header.dart';
import 'package:wallpaper/generated/l10n.dart';
import 'package:wallpaper/pages/navbar/child/mobile_list.dart';

class MobilePage extends StatefulWidget {
  final dynamic id;
  const MobilePage({super.key, required this.id});

  @override
  State<MobilePage> createState() => _MobilePageState();
}

class _MobilePageState extends State<MobilePage> with TickerProviderStateMixin {
  late TabController _tabController; // 声明 TabController
  final PageController _pageController = PageController(); // 添加这行

  @override
  void initState() {
    super.initState();
    // 初始化 TabController
    _tabController = TabController(
      length: 2,
      vsync: this,
    );
  }

  @override
  void dispose() {
    _tabController.dispose(); // 销毁控制器
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // 页面内容列表
    final List<Widget> pages = [
      MobileList(order: 'new', id: widget.id),
      MobileList(order: 'hot', id: widget.id),
    ];
    final List<String> subList = [S.of(context).a20, S.of(context).a21];
    final theme = Theme.of(context).colorScheme;

    return Scaffold(
      appBar: AppBar(
        title: const Text(''),
        toolbarHeight: 0, // 恢复默认高度
        elevation: 0,
        backgroundColor: theme.primaryContainer,
      ),
      body: Column(
        children: [
          MyTabHeader(
              tabController: _tabController,
              tabs: subList,
              onTap: (index) {
                // 点击 Tab 时切换页面
                _pageController.animateToPage(
                  index,
                  duration: const Duration(milliseconds: 300),
                  curve: Curves.ease,
                );
              }),
          Expanded(
            child: PageView(
              controller: _pageController,
              children: pages,
              onPageChanged: (index) {
                // 页面滑动时同步 Tab
                _tabController.animateTo(index);
              },
            ),
          ),
        ],
      ),
    );
  }
}
